package com.example.campuslink.commen.db.message

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

@Database(entities = [MessageTable::class], version = 2, exportSchema = true)
abstract class MessageData() : RoomDatabase() {
    abstract fun messageDao(): MessageDao

    val MIGRATION_1_2 = object : Migration(1, 2) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE message_table DELETE COLUMN messageBeanData TEXT")
            database.execSQL("ALTER TABLE message_table ADD COLUMN messageType TEXT")
            database.execSQL("ALTER TABLE message_table ADD COLUMN messageContent TEXT")
            database.execSQL("ALTER TABLE message_table ADD COLUMN messageImage TEXT")
            database.execSQL("ALTER TABLE message_table ADD COLUMN messageTime TEXT")
            database.execSQL("ALTER TABLE message_table ADD COLUMN invitationId TEXT")
            database.execSQL("ALTER TABLE message_table ADD COLUMN commendId TEXT")
        }
    }

    companion object {
        @Volatile
        private var INSTANCE: MessageData? = null

        fun getDatabase(context: Context): MessageData {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    MessageData::class.java,
                    "message_dataBase"
                )
                    .fallbackToDestructiveMigration(

                    )
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}
